var express = require('express');
var router = express.Router();
const shopModel = require("../src/models/shopModel");
const shopService = require('../src/services/shopService');
// router.get('/getshop', function(req, res, next) {
//   let a=shopModel.find().exec()
//   a.then(
//     (data)=>{
//      res.send(data);
//    })
// });
router.post('/',async function(req,res){
    let {image,name, address, latitude, longitude, distance, ontime,status} =   req.body;
     let data = await shopService.insert({ image, name, address, latitude, longitude, distance,ontime,status});
     res.send(data);
});

router.get('/getshop',async function(req,res){
    console.log(req.query);
    let {type,value,current,pageSize} = req.query;
    let searchParams = {};
    if(type){
        if(type == "age"){
            searchParams[type] = value;
        }else{
            searchParams[type] = {$regex:value};
        }
    }
    let data = await shopService.find({current:current || 1,pageSize:pageSize || 5},searchParams);
    console.log(data);
    res.send(data);
});
router.get('/:id',async function(req,res){
    let {id} = req.params;
    let data = await shopService.findById(id);
    res.send(data);
});

router.delete('/:id',async function(req,res){
    let {id} = req.params;
    let data = await shopService.delete(id);
    res.send(data);
});

router.put('/:id',async function(req,res){
    let {id} = req.params;
    let { image, name, address, latitude, longitude, distance, ontime,status} = req.body;
  
    let data = await shopService.update({_id:id, image, name, address, latitude, longitude, distance, ontime,status});
    console.log({_id:id, image, name, address, latitude, longitude, distance, ontime,status})
    res.send(data);
});

module.exports = router;